import HttpRequest from "./index";

interface Conversation {
    role: string;
    content: string;
}
function fetchAnswer(
    conversation: Conversation[],
    key: string = "",
    version: "v3.5" | "v4.0" = "v3.5"
) {
    let newConversation: Conversation[] = [];
    for (let i = 0; i < conversation.length; i++) {
        newConversation.push({
            role: conversation[i].role,
            content: conversation[i].content,
        });
    }
    if (version == "v3.5") {
        return fetch("https://www.13042332817.top//message", {
            method: "POST",
            headers: {
                "Content-Type": "text/plain",
                "Access-Control-Allow-Origin": "*",
            },
            body: JSON.stringify({ message: newConversation }),
        })
    }
    if (version =="v4.0"){
        return fetch("https://www.13042332817.top//message_4", {
            method: "POST",
            headers: {
                "Content-Type": "text/plain",
                "Access-Control-Allow-Origin": "*",
            },
            body: JSON.stringify({ message: newConversation, key: key }),
        })
    }
}
function fetchBingQuery(query: string) {
    return fetch(
        "https://www.13042332817.top//search"
        ,{
        method: "POST",
        headers: {
            "Content-Type": "text/plain",
            "Access-Control-Allow-Origin":"*"
        },
        body: JSON.stringify({ message: query, stop_summary: true }),
    });
}
function getAnswer(conversation: Conversation[]) {
    let newConversation: Conversation[] = [];
    for (let i = 0; i < conversation.length; i++) {
        newConversation.push({
            role: conversation[i].role,
            content: conversation[i].content,
        });
    }
    return HttpRequest({
        method: "post",
        url: "/chatGPTAPI/message",
        data: { message: newConversation },
        timeout: 10000,
        responseType: "blob",
    });
}

export { getAnswer, fetchAnswer, fetchBingQuery };
